﻿using UnityEngine;
using UnityEditor;
using System.Collections;

    [CustomEditor(typeof(GameMapMaker))]
    public class GameMapMakerEditor : Editor
    {
        [MenuItem("GameMapMaker/新建地图")]
        public static void NewGameMap()
        {
            GameMapFileWindow.ShowWindow("新建地图", GameMapFileWindow.MapFileType.NewMap);
        }

        [MenuItem("GameMapMaker/载入地图")]
        public static void LoadGameMap()
        {
            string StrMapPath = EditorUtility.OpenFilePanel("选择地图", Application.dataPath + "../Public/", "xml");
           
            GameMapFileManager.Inst.LoadBlockData();
            MapData = GameMapFileManager.Inst.LoadMapData(StrMapPath);
            OnEditorDataLoad();
            InitGameMap(MapData);
        }

        [MenuItem("GameMapMaker/保存")]
        public static void SaveGameMap() 
        {
            GameMapFileManager.Inst.SaveGameMap(MapData);
        }

        [MenuItem("GameMapMaker/刷新")]
        public static void Refresh()  
        {
          GameMapFileManager.Inst.LoadBlockData();
          GameMapBlockManager.Inst.GenBlockThumb();  
        }

        public static void OnEditorDataLoad()
        {
            InspectorUI.EditorHeader = new GameMapData.GameMapDataHeader(MapData.Header);
           
        }
        public static void InitGameMap(GameMapData data) 
        {
           
            GameMapEditorPanel.Inst.Load();
            GenMapGameObject(data);
        }

        GameMapMakerSceneUI sceneGUI = new GameMapMakerSceneUI();
        public void OnSceneGUI()
        {
            sceneGUI.OnSceneGUI();
        }
        

        static GameObject MapEditorGo;
        public static void GenMapGameObject(GameMapData data) 
        {
            MapEditorGo = GameObject.Find("_GameMapEditor_");
            if (MapEditorGo != null) { DestroyImmediate(MapEditorGo); }
            MapEditorGo = new GameObject("_GameMapEditor_");
            MapEditorGo.AddComponent<GameMapMaker>();
        }


        public static GameMapData MapData;

        public static GameMapMakerInspectorUI InspectorUI = new GameMapMakerInspectorUI();

        public override void OnInspectorGUI()
        {
            InspectorUI.OnInspectorGUI();
        }
       
    }

